package com.itheima.a04xpathdemo;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.List;

public class XPathDemo3 {
    public static void main(String[] args) throws DocumentException {
        //1.创建一个解析器对象
        SAXReader saxReader = new SAXReader();
        //2.解析xml文件，并且返回文档对象
        Document document = saxReader.read(new File("sockethomework\\src\\com\\itheima\\a04xpathdemo\\person.xml"));
        //3.利用XPath方式来进行快速检索
        //两个方法
        //1.检索单个   document.selectSingleNode("路径")
        //2.检索多个   document.selectNodes("路径")

        //总结：
        //  路径里面如果只有一个/表示单级的路径
        //  路径里面如果有//表示单级的路径或者多级的路径

        //全文检索
        /*List<Element> list = document.selectNodes("//name");
        System.out.println(list.size());
        for (Element element : list) {
            System.out.println(element.getText());
        }*/

        /*List<Element> list = document.selectNodes("//person/name");
        System.out.println(list.size());
        for (Element element : list) {
            System.out.println(element.getText());
        }*/

        List<Element> list = document.selectNodes("//person//name");
        System.out.println(list.size());
        for (Element element : list) {
            System.out.println(element.getText());
        }
    }
}
